package com.yang.testservice;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.view.View;
import com.yang.service.LocalService;

public class MainActivity extends Activity {
	private Intent serviceIntent;
	private LocalService myService = null;

	private String str = "Hello";

	private boolean mIsBound = false;

	@Override
	protected void onSaveInstanceState(Bundle outState) {
		super.onSaveInstanceState(outState);
		System.out.println("MainActivity.onSaveInstanceState()");
		str = "Hello World";
		outState.putString("key", str);
	}

	@Override
	protected void onRestoreInstanceState(Bundle savedInstanceState) {
		System.out.println("MainActivity.onSaveInstanceState the key is "
				+ savedInstanceState == null ? "空" : savedInstanceState
				.getString("key"));
	}

	@Override
	public Object onRetainNonConfigurationInstance() {
		return str;
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		System.out.println("MainActivity.onCreate the key is "
				+ (savedInstanceState == null ? "空" : savedInstanceState
						.getString("key")));
		System.out
				.println("MainActivity.onCreate getLastNonConfigurationInstance "
						+ (getLastNonConfigurationInstance() != null ? "空"
								: getLastNonConfigurationInstance()));
	}

	private void startMyService() {
		serviceIntent = new Intent(MainActivity.this, LocalService.class);
		startService(serviceIntent);
	}

	public void click(View v) {
		int key = v.getId();
		switch (key) {
		case R.id.button1:
			startMyService();
			doBindService();
			break;
		case R.id.button3:
			break;
		case R.id.button2:
			doUnbindService();
			break;
		case R.id.button4:
			this.finish();
			break;
		default:
			break;
		}

	}

	void doBindService() {
		// Establish a connection with the service. We use an explicit
		// class name because we want a specific service implementation that
		// we know will be running in our own process (and thus won't be
		// supporting component replacement by other applications).
		bindService(serviceIntent, serviceConnection, Context.BIND_AUTO_CREATE);
		mIsBound = true;
	}

	void doUnbindService() {
		if (mIsBound) {
			// Detach our existing connection.
			unbindService(serviceConnection);
			mIsBound = false;
		}
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		doUnbindService();
	}

	@Override
	protected void onStart() {
		super.onStart();
		// bindService(serviceIntent, serviceConnection,
		// Context.BIND_AUTO_CREATE);
	}

	ServiceConnection serviceConnection = new ServiceConnection() {

		public void onServiceConnected(ComponentName name, IBinder service) {
			myService = ((LocalService.LocalBinder) service).getService();
		}

		public void onServiceDisconnected(ComponentName name) {
			myService = null;
		}

	};

}
